﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BankSystem
{
    public partial class Form1 : Form
    {
        private ATM atm;
        private String currentAccount;
        private event EventHandler BigMoneyFetched;
        private bool affirm;

        public Form1()
        {
            InitializeComponent();

            comboBox1.SelectedIndex = 0;

            atm = new ATM();

            atm.AddBank(new Bank("第一银行"));
            atm.AddBank(new Bank("第二银行"));
            atm.AddBank(new Bank("第三银行"));

            BigMoneyFetched += (s, e) =>
            {
                var btn2 = MessageBoxButtons.OKCancel;
                var msg = "注意！您正在取走大量余额！\n" +
                    atm.Banks[comboBox1.SelectedIndex].AccountInfo(currentAccount);
                var msgbox = MessageBox.Show(msg,"取消", btn2);
                if (msgbox == DialogResult.OK) { 
                    affirm = true;
                }
                else
                {
                    affirm = false;
                }
            };
        }

        private void button1_Click(object sender, EventArgs e)
        {
            showMianPage(false);
            showCreatePage(true);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            showMianPage(true);
            showCreatePage(false);
        }

        private void showMianPage(bool visiable)
        {
            button1.Visible = visiable;
            button2.Visible = visiable;
            label1.Visible = visiable;
            comboBox1.Visible = visiable;
        }

        private void showCreatePage(bool visiable) {
            textBox1.Text = "";
            panel1.Visible = visiable;
            panel1.Location = new System.Drawing.Point(106, 185);
        }

        private void showLoginPage(bool visiable) {
            textBox2.Text = "";
            textBox3.Text = "";
            panel2.Visible = visiable;
            panel2.Location = new System.Drawing.Point(106, 185);
        }

        private void showOperatePage(bool visiable)
        {
            textBox4.Text = "";
            comboBox2.SelectedIndex = 0;
            panel3.Visible = visiable;
            panel3.Location = new System.Drawing.Point(106, 185);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var password = textBox1.Text;
            var accountNum = atm.Banks[comboBox1.SelectedIndex].AddAccount(password);
            var msgbox = MessageBox.Show("创建成功！\n您的账号为:"+accountNum);
            if (msgbox != DialogResult.Cancel) {
                showMianPage(true);
                showCreatePage(false);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            showMianPage(false);
            showLoginPage(true);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            showMianPage(true);
            showLoginPage(false);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var accountNum = textBox2.Text;
            var password = textBox3.Text;
            if (atm.Banks[comboBox1.SelectedIndex].CheckAccount(accountNum, password))
            {
                currentAccount = textBox2.Text;
                showLoginPage(false);
                showOperatePage(true);
            }
            else
            {
                var msgbox = MessageBox.Show("密码错误或账号不存在！");
                if (msgbox != DialogResult.Cancel)
                {
                    showLoginPage(true);
                }
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            showOperatePage(false);
            showMianPage(true);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            var msg = MessageBox.Show(atm.Banks[comboBox1.SelectedIndex].AccountInfo(currentAccount));
        }

        private void button7_Click(object sender, EventArgs e)
        {
            var amount = int.Parse(textBox4.Text);
            if(comboBox2.SelectedIndex == 0)
            {
                if(amount > 10000)
                {
                    BigMoneyFetched(this, null);
                    if (!affirm)
                    {
                        textBox4.Text = "";
                        return;
                    }
                }
                if (amount < atm.Banks[comboBox1.SelectedIndex].GetBalance(currentAccount))
                {
                    atm.Banks[comboBox1.SelectedIndex].SetBalance(currentAccount, -1, amount);
                    MessageBox.Show("操作成功！");
                }
                else
                {
                    MessageBox.Show("余额不足！");
                }
            }
            else
            {
                var temp = (new Random()).Next(0, 100);
                try
                {
                    if (temp < 30)
                        throw new BadCashException("存款失败！含有坏钞！");
                    atm.Banks[comboBox1.SelectedIndex].SetBalance(currentAccount, 1, amount);
                    MessageBox.Show("操作成功！");
                }catch(BadCashException ex)
                {
                MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
